package com.ruoyi.delivery.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.delivery.domain.DelDeliveryOrderBatch;
import com.ruoyi.delivery.service.IDelDeliveryOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/delivery/deliveryOrder")
public class DelDeliveryOrderController {

    @Autowired
    private IDelDeliveryOrderService deliveryOrderService;

    /**
     * 根据条件分页查询配送单信息
     *
     * @param deliveryId    配送单号
     * @param origin        发源地
     * @param state         状态
     * @param startDate     创建时间开始范围
     * @param endDate       创建时间结束范围
     * @param vehicleNumber 车辆编码
     * @param deliveryType  配送单类型
     * @param pageSize      分页大小
     * @return 配送单信息列表
     */
    @RequestMapping("/list")
    public Map<String, Object> getDeliveryOrders(
            @RequestParam(required = false) String deliveryId,
            @RequestParam(required = false) String origin,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String vehicleNumber,
            @RequestParam(required = false) String deliveryType,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        String logisticsDriver = String.valueOf(SecurityUtils.getUserId());
        int offset = (page - 1) * pageSize;
        List<DelDeliveryOrderBatch> deliveryOrders = deliveryOrderService.getDeliveryOrders(logisticsDriver, deliveryId, origin, state, startDate, endDate, vehicleNumber, deliveryType, offset, pageSize);
        int total = deliveryOrderService.getDeliveryOrderCount(logisticsDriver, deliveryId, origin, state, startDate, endDate, vehicleNumber, deliveryType);

        Map<String, Object> response = new HashMap<>();
        response.put("data", deliveryOrders);
        response.put("total", total);
        response.put("page", page);
        return response;
    }

    /**
     * 新增配送单信息
     * @param deliveryOrder 配送单信息对象
     * @return 插入影响的行数
     */
    @RequestMapping("/add")
    public int addDeliveryOrder(DelDeliveryOrderBatch deliveryOrder) {
        return deliveryOrderService.insertDeliveryOrder(deliveryOrder);
    }

    /**
     * 删除单个配送单
     * @param deliveryId 配送单号
     * @return 删除影响的行数
     */
    @RequestMapping("/delete")
    public int deleteDeliveryOrder(String deliveryId) {
        return deliveryOrderService.deleteDeliveryOrder(deliveryId);
    }

    /**
     * 更新配送单信息
     * @param deliveryOrderBatch 配送单信息对象
     * @return 更新影响的行数
     */
    @PutMapping("/update")
    public int updateDeliveryOrder(@RequestBody DelDeliveryOrderBatch deliveryOrderBatch) {
        return deliveryOrderService.updateDeliveryOrderInfo(deliveryOrderBatch);
    }
}
